package core;

import java.io.File;
import java.io.IOException;

import sha1.*;
import commander.*;
import gitobject.*;
import fileoperation.FileDeletion;
import repository.Repository;

public class JitInit {
	/**
	 * Init repository in your working area. The workTree should never be null.
	 * @param workTree
	 * @throws IOException
	 */
    //重置工作区，输入工作区路径workTree，默认workTree不为空
    public static void init(String workTree) throws IOException {
        Repository repo = new Repository(workTree);   //建立仓库类对象repo，工作路径为workTree
        if(repo.exist()){ //repo为文件夹时初始化工作路径
            if(repo.isDirectory()){
                FileDeletion.deleteFile(Repository.getGitDir());
            }
            else if(repo.isFile()){//repo为文件时报错
                throw new IOException(".jit is a file, please check");
            }
        }
        repo.createRepo();//调用repository类中repo对象的creatRepo()函数创建文件夹
        System.out.println("Jit repository has been initiated successfully.");
    }
    /**
     * @Test test the Jitinit() function
     *
     * @param args
     */
 /*   public static void main(String[] args) {
        try{
            new JitInit().init("C:/Users/之靈/Desktop/Git");
            System.out.println(Repository.getGitDir());
            System.out.println(Repository.getWorkTree());
            File f1 = new File(Repository.getWorkTree() + File.separator + "test.txt");
            System.out.println(f1.exists());
            String key = SHA1.getHash(f1);
            System.out.println(key);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
